using System;
using System.Collections.Generic;
using System.Text;
using CodePlex.Resourceful.Amazon.S3;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using CodePlex.Resourceful.Amazon.S3.Test;
using System.Text.RegularExpressions;
using CodePlex.Resourceful.Amazon.S3.Actions;
using System.Net;
using CodePlex.Resourceful.Sun.CloudStorage;
using CodePlex.Resourceful.Microsoft.Azure;
using System.Xml;

namespace CodePlex.Resourceful.Examples
{
    public static class AzureQueueExample
    {

      
        public static void Run()
        {

            string storage = Environment.GetEnvironmentVariable("AZURE");

            string accountName = storage.Split(':')[0];
            string base64key = storage.Split(':')[1];


            AzureQueueClient client = new AzureQueueClient(accountName, base64key);
            client.Tracer.AddListener(Console.Out);

            // delete all queues
            //AzureQueueConnection cn = new AzureQueueConnection(client);
            //foreach (QueueInfo queue in cn.ListQueues().Queues)
            //    cn.DeleteQueue(queue.QueueName);

            string queueName =  Guid.NewGuid().ToString();

            client.PutQueue(queueName, null);

            client.PostMessage(queueName, "helloworld", null);
            string message = client.GetMessages(queueName, null, null).AsString();
            string messageId = Regex.Match(message, "<MessageId>(.*?)</MessageId>").Groups[1].Value;
            string popReceipt = Regex.Match(message, "<PopReceipt>(.*?)</PopReceipt>").Groups[1].Value;
            client.DeleteMessage(queueName, messageId, popReceipt);
           
            client.GetQueues(null, null, null);
            client.DeleteMessages(queueName);
            client.DeleteQueue(queueName);


            
        }


        private static void DumpXml(ClientResponse response)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(response.AsXmlReader());

            Utils.PrettyPrint(doc, Console.Out);
        }


    }
}
